
<?php
class ultimate_series_module extends CI_Module {
	public function init() {
		$this->load->model("mgame");
		$this->load->model("mcategory");
	}
	
	public function active() {
		
	}
	
	public function deactive() {
		
	}
	
	public function admin($config) {
	
		//$title = $config['title'];
		//$wconfig = $config['config'];
		//$layout = $config['layout'];
		
		$config['config']['list_cate'] = $this->mcategory->get_cat_tree();
		if (isset($config['config']['cateid']) && $config['config']['cateid'])
			$config['config']['cateid'] = explode(',', $config['config']['cateid']);
		$this->view('admin', $config['config']);
	}
	
	public function module_update($data) {
		if (isset($data['cateid']) && $data['cateid']) {
			$data['cateid'] = implode(',', $data['cateid']);
		}else {
			$data['cateid']  = '';
		}
		return $data;
	}
	/**
	 * 
	 * @param object $config
	 * 
	 */
	public function run($config) {
		$arr_default = array();
		$arr_default['title_link'] = '';
		$arr_default['limit'] = '6';
		$arr_default['cateid'] = '';
		$arr_default['show_current_category'] = 'false';
		$arr_default['order_by'] = 'game_id';
		$arr_default['order'] = 'DESC';
		$config = array_merge($arr_default,$config);
		if($config['limit'] >0){
			$this->db->limit($config['limit']);
		}
		
		if($config['order_by'] == 'random'){
			$this->db->order_by('RAND()');
		}
		else {
			$this->db->order_by("game.".$config['order_by'].' '.$config['order']);
		}
		$config['cateid'] = explode(",",$config['cateid']);
		if($config['show_current_category'] =="true")
			$config['cateid'] = array_merge($config['cateid'], (array)$this->msystem->get_global('cat_id'));
		$config['cateid'] = array_filter($config['cateid'], 'strlen');
		
		$this->db->where("game_status >",0);
		if($config['cateid'] && count($config['cateid']) >0 ){
			$config['games'] = $this->mgame->get_by_cate($config['cateid']);
		}
		else {
			$config['games'] = $this->mgame->gets();
		}
		$view = 'home';
		if($config['widget_layout'] =='playgame' && $config['widget_position'] =='mid')
			$view = 'playgame_mid';
		
		$this->view($view, $config);
	}
	
	function new_gameAction($param){
		$data = array();
		$data['title'] = 'Game mới nhất';
		$limit = $param[0];
		$page  = $param[1];
		$page = ($page <= 0) ? 1 : $page;
		$config = array();
		$config['per_page'] = $limit;
		$config['cur_page'] = $page;
		$config['base_url'] = base_url().'game-moi-nhat';
		$config['total_rows'] = $this->mgame->num('0','game_status >');
		$this->pagination->initialize($config);
		
		$this->db->order_by('game_id desc');
		$data['games'] =  $this->mgame->pagination('0','game_status >',$config);
		$data['seo'] = $this->sseo->get('game-moi-nhat');
		$data['canonical'] = base_url();
		$theme_data = array(
			'layout_main' => 'frontend/layout/home',
			'left' => 'frontend/include/left',
			'right' => 'frontend/include/right',
			'main' => 'frontend/uindex/game_list',
		);
		$this->template->view($theme_data, $data);
	}
	
	function play_moreAction($param){
		$data = array();
		$data['title'] = 'Game chơi nhiều';
		$limit = $param[0];
		$page  = $param[1];
		$page = ($page <= 0) ? 1 : $page;
		$config = array();
		$config['per_page'] = $limit;
		$config['cur_page'] = $page;
		$config['base_url'] = base_url().'game-choi-nhieu';
		$config['total_rows'] = $this->mgame->num('0','game_status >');
		$this->pagination->initialize($config);
		
		$this->db->order_by('game_view desc');
		$data['games'] =  $this->mgame->pagination('0','game_status >',$config);
		$data['seo'] = $this->sseo->get('game-choi-nhieu');
		$data['canonical'] = base_url();
		$theme_data = array(
			'layout_main' => 'frontend/layout/home',
			'left' => 'frontend/include/left',
			'right' => 'frontend/include/right',
			'main' => 'frontend/uindex/game_list',
		);
		$this->template->view($theme_data, $data);
		
	}
}